package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.entity.vo.CartVO;
import com.cy.store.mapper.CartMapper;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.ICartService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addCart(Cart cart, String username, Integer uid) {
        Cart result = cartMapper.findByPidAndUid(cart.getPid(), uid);
        Product product = productMapper.findById(cart.getPid());
        cart.setPrice(product.getPrice());
        cart.setUid(uid);
        cart.setModifiedTime(new Date());
        cart.setModifiedUser(username);
        if (result == null) {
            cart.setCreatedUser(username);
            cart.setCreatedTime(new Date());
            cartMapper.insert(cart);
        } else {
            cart.setNum(cart.getNum() + result.getNum());
            Integer row = cartMapper.updateNumByPidAndUid(cart);
            if (row != 1) {
                throw new UpdateException("修改购物车时产生未知异常");
            }
        }
    }

    @Override
    public List<CartVO> findByUid(Integer uid) {
        return cartMapper.findVoByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("购物车数据不存在异常");
        }
        if (!result.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum() + 1;
        Cart cart = new Cart();
        cart.setNum(num);
        cart.setModifiedTime(new Date());
        cart.setModifiedUser(username);
        cart.setUid(uid);
        cart.setPid(result.getPid());
        Integer row = cartMapper.updateNumByPidAndUid(cart);
        if (row!=1){
            throw new UpdateException("修改数据时产生异常");
        }
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("购物车数据不存在异常");
        }
        if (!result.getUid().equals(uid)) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getNum() - 1;
        Cart cart = new Cart();
        cart.setNum(num);
        cart.setModifiedTime(new Date());
        cart.setModifiedUser(username);
        cart.setUid(uid);
        cart.setPid(result.getPid());
        Integer row = cartMapper.updateNumByPidAndUid(cart);
        if (row!=1){
            throw new UpdateException("修改数据时产生异常");
        }
        return num;
    }

    @Override
    public List<CartVO> findVoByCid(Integer uid,Integer[] cids) {
        List<CartVO> cartVOList = cartMapper.findVoByCid(cids);
        Iterator<CartVO> iterator = cartVOList.iterator();
        while(iterator.hasNext()){
            CartVO cartVO = iterator.next();
            if (!cartVO.getUid().equals(uid)){
               iterator.remove();
            }
        }
        return cartVOList;
    }
}
